package com.jxaummd.mushroom.main.common.presenter;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.core.base.data.CommonBackModel;
import com.jxaummd.mushroom.core.base.data.DataSource;
import com.jxaummd.mushroom.core.base.presenter.BasePresenter;
import com.jxaummd.mushroom.main.common.contract.SignUpSendCodeContract;
import com.jxaummd.mushroom.main.common.data.SendCodeData;
import com.jxaummd.mushroom.main.common.fragment.SignUpFragment;


public class SignUpSendCodePresenter extends BasePresenter<SignUpSendCodeContract.View>
        implements SignUpSendCodeContract.Presenter,DataSource.Callback<CommonBackModel>{

    public SignUpSendCodePresenter(SignUpSendCodeContract.View view) {
        super(view);
    }
    private String phoneNumber = null;


    @Override
    public void sendCode(String phoneNumber) {
        if(TextUtils.isEmpty(phoneNumber)||phoneNumber.length()<11){
            ToastUtils.showShort(Utils.getApp().getResources().getString(R.string.common_signup_phonenumber_nocooect));
        }else {
            //发送验证码
            SendCodeData.sendRegeisterCode(phoneNumber,this);
            this.phoneNumber=phoneNumber;
        }
    }

    @Override
    public void start() {
        super.start();
        getView().initText(getSpannableSreing());
    }


    /**
     *获取
     * @return
     */
    SpannableString getSpannableSreing(){
        String informSafe = Utils.getApp().getResources().getString(R.string.app_contract_person_inform_safe);
        String serveUser = Utils.getApp().getResources().getString(R.string.app_contract_serve_user);
        String indextext = Utils.getApp().getResources().getString(R.string.common_signup_sendcode_contract);
        String and = Utils.getApp().getResources().getString(R.string.common_signup_sendcode_and);
        int informSafeLength = informSafe.length();
        int serveUserLength = serveUser.length();

        SpannableString hintInfo = new SpannableString(indextext+informSafe+and+serveUser);

        hintInfo.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(View widget) {
//                getView().startNewFragment(WebViewFragment.newInstance("https://www.baidu.com/?tn=80035161_1_dg"));
            }
        },indextext.length(),indextext.length()+informSafeLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


        hintInfo.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(View widget) {

            }
        },indextext.length()+informSafeLength+1,indextext.length()+informSafeLength+1+serveUserLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return hintInfo;
    }


    @Override
    public void onDataNotAvailable(int strRes) {

    }

    @Override
    public void onDataLoaded(CommonBackModel commonBackModel) {
        ToastUtils.showShort(commonBackModel.getMessage());
        if(commonBackModel.getStatus()==200){
            //发送成功
            getView().startNewFragment(SignUpFragment.newInstance(phoneNumber));
        }

    }
}
